namespace Warehouse

open System
open System.Windows.Data
open Warehouse.Controls

open FSharp.Windows

type ProductSearchEvents = 
    | Search

type ProductSearchPartialView(control) = 
    inherit PartialView<ProductSearchEvents, ProductSearchModel, ProductSearchControl>(control)

    override this.EventStreams = 
        [
            this.Control.Search.ClickToValue Search
        ]

    override this.SetBindings model = 
        Binding.FromExpression 
            <@ 
                this.Control.Products.ItemsSource <- model.Products
                this.Control.Products.SelectedItem <- model.SelectedProduct
                this.Control.ItemsCount.Text <- string model.Products.Count
                this.Control.Search.IsEnabled <- not model.IsSearchInProgress
            @>

        Binding.FromExpression(
            <@ this.Control.Filter.Text <- model.Filter @>, 
            BindingMode.OneWayToSource, 
            UpdateSourceTrigger.PropertyChanged)

        this.Control.Products.SetBindings(
            model,
            itemsSource = <@ model.Products @>,
            selectedItem = <@ model.SelectedProduct @>,
            columnBindings = fun item ->
                [
                    this.Control.SKU, <@@ item.SKU @@>
                    this.Control.Brand, <@@ item.ProductName.Brand.Name @@>
                    this.Control.Product, <@@ item.ProductName.Name @@>
                    this.Control.SizeOrColor, <@@ item.SizeDisplay @@>
                    this.Control.ShopCost, <@@ String.Format("{0:c}", item.ShopCost) @@>
                    this.Control.TotalQuantity, <@@ item.TotalQuantity @@>
                ]
        )